package com.hotchpotch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class RecyclerRefreshAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener{
    private Context context;
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    private int itemCount = 2;

    public RecyclerRefreshAdapter(Context context) {
        this.context = context;
    }

    /**
     * 改变条数
     */
    public void setItemCount() {
        itemCount++;
        notifyDataSetChanged();
    }

    /**
     * 点击监听
     */
    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_refresh, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).textView.setText("测试数据" + position);
        }
    }

    @Override
    public int getItemCount() {
        return itemCount;
    }

    @Override
    public void onClick(View view) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemClick(view);
        }
    }

    @Override
    public boolean onLongClick(View view) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemLongClick(view);
        }
        return false;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }
}
